package com.qywx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.qywx.dao.TagDao;
import com.qywx.dao.TagRuleDao;
import com.qywx.dao.UserDao;
import com.qywx.entity.*;
import com.qywx.service.TagRuleService;
import com.qywx.service.TagService;
import com.qywx.utils.R;
import com.qywx.utils.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TagRuleServiceImpl
 * @Description:
 * @Author wmj
 * @Date 2020/3/4
 * @Version V1.0
 **/
@Service("tagRuleService")
@Slf4j
public class TagRuleServiceImpl implements TagRuleService {

    @Autowired
    TagRuleDao tagRuleDao;

    @Override
    @Transactional
    public R saveTagRule(TagRuleEntity tagEntity) {
        try {
            //确保属性1到10相同的情况下，tagid不能重复插入
            List<Long> data = tagRuleDao.selectByAttribute(transferTagRule(tagEntity));
            if(data != null && data.size() != 0){
                return  R.error("tagId重复，不允许重复添加!");
            }
            int i = tagRuleDao.insert(tagEntity);
        }catch (Exception e){
            log.error("TagRuleServiceImpl saveTagRule exception:{} ",e.getMessage());
            return R.error(e.getMessage());
        }
        return R.ok();

    }

    UserEntity transferTagRule(TagRuleEntity tagRuleEntity){
        UserEntity userEntity =  new UserEntity();
        if(tagRuleEntity != null){
            userEntity.setAttribute1(tagRuleEntity.getAttribute1());
            userEntity.setAttribute2(tagRuleEntity.getAttribute2());
            userEntity.setAttribute3(tagRuleEntity.getAttribute3());
            userEntity.setAttribute4(tagRuleEntity.getAttribute4());
            userEntity.setAttribute5(tagRuleEntity.getAttribute5());
            userEntity.setAttribute6(tagRuleEntity.getAttribute6());
            userEntity.setAttribute7(tagRuleEntity.getAttribute7());
            userEntity.setAttribute8(tagRuleEntity.getAttribute8());
            userEntity.setAttribute9(tagRuleEntity.getAttribute9());
            userEntity.setAttribute10(tagRuleEntity.getAttribute10());
        }
        return userEntity;

    }

    @Override
    @Transactional
    public R updateTagRule(TagRuleEntity tagEntity) {
        if(tagEntity == null || tagEntity.getId() == 0L)
            return R.error("未传递ID!");
        try{
            tagRuleDao.updateTagRuleById(tagEntity);
        }catch (Exception e){
            log.error("TagRuleServiceImpl updateTagRule exception:{} ",e.getMessage());
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    @Override
    public R findTagRuleByTagId(long tagId,int page,int size) {
        if(page <=0){
            page = 1;
        }
        List<TagRuleEntity> lists = Lists.newArrayList();
        Long total  = 0L;
        try{
            lists = tagRuleDao.findByTagId(tagId,(page-1)*size,size);
            total = tagRuleDao.getTotal(tagId);
        }catch (Exception e){
            log.error("TagRuleServiceImpl findTagRuleByTagId exception:{} ",e.getMessage());
            return R.error(e.getMessage());
        }
        R r = new R();
        r.put("data", lists);
        r.put("total", total);
        return r;

    }
}
